package com.adong.crowd.mvc.handler;

import com.adong.crowd.constant.CrowdConstant;
import com.adong.crowd.entity.Admin;
import com.adong.crowd.exception.LoginFailedException;
import com.adong.crowd.mapper.AdminMapper;
import com.adong.crowd.service.api.AdminService;
import com.adong.crowd.util.CrowdUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * 处理Admin请求的Controller
 */
@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    /**
     * 用来登录账号的
     *
     * @param loginAcct
     * @param userPswd
     * @param session
     * @return
     */
    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session) {
        /**
         * 合法的判断
         */
        if (CrowdUtil.isEmpty(loginAcct)) {
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_NOT_USER);
        }
        if (CrowdUtil.isEmpty(userPswd)) {
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_NOT_PSWD);
        }

        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        // 解决重复提交：重定向到admin/to/main/page.html ,admin/to/main/page.html在跳转带跳转成功页面
        return "redirect:/admin/to/main/page.html";
    }

    /**
     * 推出管理员登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session) {
        session.invalidate();
        // 重定向到登陆页面 ,这些配置在mvc中的mvc-controller
        return "redirect:/admin/to/login/page.html";
    }



    /**
     * 处理查询和分页Admin
     * http://localhost:8080/crowd/admin/get/page.html?pageNum=1&keyword=b
     * 使用@RequestParam注解的defaultValue属性，指定默认值，在请求中没有携带对应参数时使用默认值
     *
     * @param keyword  keyword默认值使用空字符串，和SQL语句配合实现两种情况适配
     * @param pageNum  pageNum默认值使用1,当前第几页
     * @param pageSize pageSize默认值使用5，每页显示几条数据
     * @param map      用来存储数据的
     * @return
     */
    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            ModelMap map) {

        //查询admin数据集合
        PageInfo<Admin> pageInfo = adminService.getAdminPage(keyword, pageNum, pageSize);
        //将分页存入模型
        map.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }

    /**
     * 删除元素且携带分页数据，和搜索关键字
     *
     * @param adminId 删除的用户id
     * @param pageNum 当前的页码
     * @param keyword 搜索的关键字
     * @return
     */
    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword) {

        // 删除数据库中数据
        adminService.removeAdmin(adminId);

        /*
        1.不能跳转会admin-page页面没有数据
        2.也不能转发到/admin/get/page.html页面刷新会重新执行删除
        3.重定向带/admin/get/page.html地址，并且把请求参数过去
        */
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    /**
     * 保存Admin方法.
     */
    @RequestMapping("/admin/save.html")
    public String save(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum=99999";
    }

    /**
     * 去编辑按钮,查询出数据
     * admin/to/edit/page.html?adminId=${admin.id }&pageNum=${requestScope.pageInfo.pageNum }&keyword=${param.keyword }
     */
    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            ModelMap modelMap) {
        Admin admin = adminService.getAdminById(adminId);
        modelMap.addAttribute("admin", admin);
        return "admin-edit";

    }

    /**
     * 更新按钮
     *
     * @param admin
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("/admin/update.html")
    public String update(Admin admin, @RequestParam("pageNum") Integer pageNum, @RequestParam("keyword") String keyword) {

        adminService.updateAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


}



